<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单选下拉选择框-01</title>
</head>
<style>
    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }
    
    ul {
        list-style: none;
        width: 180px;
    }
    
    ul li {
        border: 1px solid #ffff33;
    }
    
    .category {
        display: none;
    }
    
    .category input[type='radio'] {
        display: inline-block;
        width: 16px;
        height: 16px;
        margin-right: 8px;
    }
    /* 定位下拉列表在选择框下面 */
    
    .single {
        position: relative;
        margin-top: 30px;
        margin-left: 60px;
    }
    
    .single>input {
        width: 180px;
        height: 42px;
        padding: 2px 5px;
        border: 1px solid #000033;
    }
    
    .single .category {
        position: absolute;
        top: 42px;
        left: 0;
    }
    /* 定位二级列表出现在一级右侧 */
    
    .single>ul>li {
        position: relative;
    }
    
    .single .subCategory {
        position: absolute;
        left: 180px;
        top: -1px;
    }
    /* 缩进 */
    
    .single .level1,
    .single .level2 {
        display: inline-block;
        width: 180px;
        height: 36px;
        line-height: 36px;
        background-color: rgba(0, 255, 00, 0.5);
        padding-left: 10px;
    }
    
    .single .level1:hover,
    .single .level2:hover {
        background-color: rgba(0, 255, 00, 0.8);
    }
    
    .subCategory {
        display: none;
    }
    
    .active {
        display: inline-block;
    }
    
    .checked {
        background-color: rgba(0, 255, 00, 0.8);
    }
</style>
<script>
    window.onload = function() {
        var radios = document.querySelectorAll('.category input[type="radio"]');
        var select = document.querySelector(".single input[type='text']");
        var category = document.querySelector('.category');
        // 取消radio的选中
        function
        clear_radio_checked() {
            for (var i = 0; i < radios.length; i++) {
                radios[i].checked = false;
                if (radios[i].parentElement.classList.contains('checked')) {
                    radios[i].parentElement.classList.remove('checked');
                }
                if (radios[i].parentElement.nextElementSibling) {
                    if (radios[i].parentElement.nextElementSibling.classList.contains('active')) {
                        radios[i].parentElement.nextElementSibling.classList.remove('active');
                    }
                }
            }
        }

        for (var i = 0; i < radios.length; i++) {
            radios[i].onclick = function() {
                // console.log(this);
                clear_radio_checked();
                this.checked = true;
                if (this.parentElement.classList.contains('level1')) {
                    this.parentElement.classList.add('checked');
                }
                if (this.parentElement.nextElementSibling) {
                    this.parentElement.nextElementSibling.classList.add('active')
                }
                if (this.parentElement.classList.contains('level2')) {
                    this.parentElement.parentElement.parentElement.classList.add('active');
                }
                select.value = this.parentElement.innerText;
            }
        }

        // select.addEventListener('click', function(e) {
        //     category.style.display = 'block'
        // })
        document.querySelector('.single').addEventListener('click', function(e) {
            console.log(e.target, e.target.nodeName.toLowerCase())
            if (e.target) {
                var elems = ['ul', 'li', 'div', 'input'];
                var cur_node = e.target.nodeName.toLowerCase();
                elems.some(function(elem, index, elems) {
                    if (elem == cur_node) {
                        category.style.display = 'block'
                    }
                })

                if (cur_node == 'div' && cur_node == 'li' && cur_node == 'input') {

                }
            }
        })




    }
</script>

<body>
    <div class="single">
        <input type="text">
        <ul class="category">
            <li>
                <div class="level1">
                    <input type="radio">AA
                </div>
            </li>
            <li>
                <div class="level1"><input type="radio">BB
                </div>
                <ul class="subCategory">
                    <li>
                        <div class="level2"><input type="radio">BB1

                        </div>
                    </li>
                    <li>
                        <div class="level2"><input type="radio">BB2

                        </div>
                    </li>
                    <li>
                        <div class="level2"><input type="radio">BB3

                        </div>
                    </li>
                </ul>
            </li>
            <li>
                <div class="level1"><input type="radio">CC
                </div>
                <ul class="subCategory">
                    <li>
                        <div class="level2"><input type="radio">CC1

                        </div>
                    </li>
                    <li>
                        <div class="level2"><input type="radio">CC2

                        </div>
                    </li>
                    <li>
                        <div class="level2"><input type="radio">CC3

                        </div>
                    </li>
                    <li>
                        <div class="level2"><input type="radio">CC4

                        </div>
                    </li>
                </ul>

            </li>
            <li>
                <div class="level1"><input type="radio">DD

                </div>
            </li>
            <li>
                <div class="level1"><input type="radio">EE

                </div>
            </li>
            <li>
                <div class="level1"><input type="radio">FF

                </div>
            </li>
        </ul>
    </div>

</body>

</html>

</html>